import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@abp/components';
import {
  ArticleSourceInfoServiceProxy,
  ArticleSourceInfoListDto,
} from '@abp/service-proxies/service-proxies';
import { CreateOrEditArticleSourceComponent } from './create-or-edit-article-source/create-or-edit-article-source.component';

@Component({
  templateUrl: './article-source.component.html',
  styleUrls: ['./article-source.component.less'],
})
export class ArticleSourceComponent extends AppComponentBase {
  /** 是否显示高级过滤 */
  advancedFiltersVisible = false;

  /** 排序 */
  sortValue = null;
  sortKey = null;

  /** 加载 */
  skeletonLoading = true;
  switchloading = false;

  /** 模糊搜索的文本 */
  filterText = '';
  /** 日期搜索 */
  createStartToEndDate = [];
  updateStartToEndDate = [];

  constructor(
    injector: Injector,
    private articleSourceService: ArticleSourceInfoServiceProxy,
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.searchData();
  }

  /**
   * 刷新表格数据
   * @param args
   */
  refresh(): void {
    this.searchData();
  }

  sort(sort: { key: string; value: string }): void {
    this.tableHelper.sort = sort;
    this.searchData();
  }

  dateRangeChange() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    if (reset) {
      this.tableHelper.pageIndex = 1;
    }
    this.tableHelper.isLoading = true;

    this.articleSourceService
      .getArticleSourceInfos(
        false,
        this.createStartToEndDate.length === 2
          ? this.createStartToEndDate[0] === null
            ? undefined
            : this.createStartToEndDate[0]
          : undefined,
        this.createStartToEndDate.length === 2
          ? this.createStartToEndDate[1] === null
            ? undefined
            : this.createStartToEndDate[1]
          : undefined,
        this.updateStartToEndDate.length === 2
          ? this.updateStartToEndDate[0] === null
            ? undefined
            : this.updateStartToEndDate[0]
          : undefined,
        this.updateStartToEndDate.length === 2
          ? this.updateStartToEndDate[1] === null
            ? undefined
            : this.updateStartToEndDate[1]
          : undefined,
        this.filterText,
        this.tableHelper.getSorting(),
        this.tableHelper.getMaxResultCount(),
        this.tableHelper.getSkipCount(),
      )
      .subscribe(data => {
        this.tableHelper.isLoading = false;
        this.tableHelper.totalRecordsCount = data.totalCount;
        this.tableHelper.records = data.items;
      });
  }

  createBuildingPlan(): void {
    let config = this.drawerConfig(
      '添加用户',
      CreateOrEditArticleSourceComponent,
      {
        id: undefined,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  editBuildingPlan(data: ArticleSourceInfoListDto): void {
    let config = this.drawerConfig(
      '修改用户:' + data.name,
      CreateOrEditArticleSourceComponent,
      {
        id: data.id,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  deleteConfirm(id: number): void {
    this.articleSourceService.deleteArticleSourceInfo(id).subscribe(() => {
      this.searchData();
      this.notify.success('删除成功！');
    });
  }
}
